﻿using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration;
using ServBus.ListForm2010.Controls;
using ServBus.ListForm2010.License;
using ServBus.ListForm2010.Resources;
using ServBus.ListForm2010.Utility;

namespace ServBus.ListForm2010.AdministrationPages
{
    public partial class ImportLicense : System.Web.UI.Page
    {
        #region Controls
        protected BreadCrumbNav bcNav;
        protected FileUpload fileLicense;
        protected Label lblError;
        #endregion

        private static DllResource Resource = new DllResource("ServBus.ListForm2010.Common.Resources|AdminPages");

        protected void Page_Load(object sender, EventArgs e)
        {
            bcNav.AddLink(Resource.GetString("License_Nav"), "License.aspx", null);
            bcNav.AddLink(Resource.GetString("Import_Nav"), null, null);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (fileLicense.FileBytes == null || fileLicense.FileBytes.Length == 0)
            {
                lblError.Text = Resource.GetString("Import_Error");
                return;
            }

            SPFarm farm = SPFarm.Local;
            
            farm.Properties[Constants.FARM_LICENSE_KEY] = Convert.ToBase64String(fileLicense.FileBytes);
            farm.Update();

            //foreach (string key in farm.Properties.Keys)
            //{
            //    lblError.Text += "[" + key + "]" + farm.Properties[key];
            //}
            
            LicenseContainer.UpdateLicense();

            Page.Response.Redirect("License.aspx", false);
        }
    }
}
